<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入盘点任务名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点人员" prop="userId">
        <el-select v-model="queryParams.userId" placeholder="请选择" clearable>
          <el-option
            v-for="dict in userList"
            :key="dict.user_id"
            :label="dict.user_name"
            :value="dict.user_id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点任务状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择" clearable>
          <el-option
            v-for="dict in asset_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="资产类型" prop="assetType">
        <el-select v-model="queryParams.assetType" placeholder="请选择资产类型" clearable>
          <el-option
            v-for="dict in fixed_assets_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['plat:assetsInventory:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['plat:assetsInventory:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table class="fixed-scrollbar" v-horizontal-scroll border v-loading="loading" :data="list" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="盘点任务名称" align="center" prop="name" min-width="120" show-overflow-tooltip />
        <el-table-column label="盘点日期" align="center" width="200">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
            <span> - </span>
            <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="盘点公司" align="center" prop="companyName" min-width="150" show-overflow-tooltip />
        <el-table-column label="盘点部门" align="center" prop="departName" width="120" show-overflow-tooltip />
        <el-table-column label="盘点人员" align="center" prop="userName" width="120" />
        <el-table-column label="资产类型" align="center" prop="assetType" width="120">
          <template #default="scope">
            <dict-tag :options="fixed_assets_type" :value="scope.row.assetType"/>
          </template>
        </el-table-column>
        <el-table-column label="盘点开始时间" align="center" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="盘点结束时间" align="center" width="160">
          <template #default="scope">
            <span>{{ parseTime(scope.row.completedTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="盘点任务状态" align="center" width="120">
          <template #default="scope">
            <dict-tag :options="asset_task_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="110" fixed="right">
          <template #default="scope">
            <el-tooltip content="详情" placement="top">
              <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['plat:assetsInventory:query']"></el-button>
            </el-tooltip>
            <template v-if="scope.row.status == 1">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['plat:assetsInventory:edit']"></el-button>
              </el-tooltip>
            </template>
            <template v-if="scope.row.status == 1">
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['plat:assetsInventory:remove']"></el-button>
              </el-tooltip>
            </template>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产盘点任务对话框 -->
    <Dialog v-model="showDialog" :title="dialogTitle" width="50%">
      <edit-task 
        ref="editTask"
        @cancel="cancel"
        @confirm="confirm"
      />
    </Dialog>
  </div>
</template>

<script setup name="Task">
import { listAssetsInventory, getAssetsInventory, delAssetsInventory, addAssetsInventory, updateAssetsInventory, getSelectData } from "@/api/asset/task"
import { getAllCompanies } from '@/api/company/company'
import { nextTick } from "vue"
import { useRouter } from "vue-router"
import EditTask from './edit.vue'

const router = useRouter()
const { proxy } = getCurrentInstance()
const { fixed_assets_type, asset_task_status } = proxy.useDict('fixed_assets_type', 'asset_task_status')

const editTask = ref()
const list = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const userList = ref([])
const showDialog = ref(false)
const dialogTitle = ref('')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    status: null,
    startDate: null,
    endDate: null,
    companyId: null,
    departId: null,
    userId: null,
    assetType: null,
  }
})

const { queryParams } = toRefs(data)

/** 获取盘点员列表 */
function getUserList() {
  getSelectData().then(response => {
    userList.value = response.data.user
  })
}

/** 查询资产盘点任务列表 */
function getList() {
  loading.value = true
  listAssetsInventory(queryParams.value).then(response => {
    list.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
}

function confirm() {
  cancel()
  getList()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 查看详情 */
function handleDetail(row) {
  router.push(`/asset/taskdetail/${row.id}`)
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '添加资产盘点任务'
  nextTick(() => {
    editTask.value.pageInit()
  })
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogTitle.value = '修改资产盘点任务'
  nextTick(() => {
    editTask.value.pageInit(_id)
  })
}

/** 提交按钮 */
function submitForm() {
  
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该资产盘点任务？').then(function() {
    return delAssetsInventory(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('plat/assetsInventory/export', {
    ...queryParams.value
  }, `资产盘点任务_${proxy.getFileImportName()}.xlsx`)
}

getUserList()
getList()
</script>
